<?php
Class Common extends CI_Controller{
	function upload_image(){
		try{
			$config['upload_path'] = './assets/temp/image/';
	        $config['allowed_types'] = 'jpeg|png|jpg|flv|mp3|wav';
	        $config['max_size'] = '10000';
	        
	        #up image
			$this->load->library('upload', $config);
			$this->upload->do_upload('add_image');
			$data = $this->upload->data();
			
			#resize image
			$w = 176;
        	$h = 190;
			
        	$this->load->library('image_lib');
	        $config['image_library'] = 'gd2';
	        $config['source_image'] = $data['full_path'];
	        $config['create_thumb'] = TRUE;
	        $config['maintain_ratio'] = TRUE;
	        $config['thumb_marker'] = '';
	        $config['width'] = $w;
	        $config['height'] = $h;
	        	
        	$this->image_lib->initialize($config);
        	
        	if ($data['image_width']<176 ){
        		$w = $data['image_width'];
        	}
			if ($data['image_height']<190 ){
        		$h = $data['image_height'];
        	}
        	
        	if($data['image_width']<176 && $data['image_height']<190){
	        }else{
	            $this->image_lib->resize();
	        }
	        	
	         $file = $data['file_name'];
	        echo json_encode(array("error"=>false,"msg"=>"success","file"=>$file,"dir"=>"image"));
	        
	        #link tham khao : http://stackoverflow.com/questions/9615147/upload-image-in-codeigniter-using-ajax
	        
		}
		catch(Exception $e){
			echo json_encode(array("error"=>true,"msg"=>$e->getMessage()));
		}		
	}
} 
?>